package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AnalysisStatementMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AnalysisStatement;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.AnalysisStatementService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangcs on 2017/10/27.
 */
public class AnalysisStatementServiceImpl implements AnalysisStatementService {

    @Autowired
    private AnalysisStatementMapper analysisStatementMapper;

    @Override
    public JSONArray dayList(String day) {
        Map map = new HashMap<>();
        map.put("day",day);
        JSONArray array = new JSONArray();
        JSONArray array1 = new JSONArray();
        JSONArray array2 = new JSONArray();
        JSONArray array3 = new JSONArray();
        List<AnalysisStatement> data;
        JSONArray arrayData;
        JSONObject object;
        // 第一级
        List<AnalysisStatement> list1 = analysisStatementMapper.queryAnalysisCell(map);
        if( list1 != null && list1.size() > 0 ){
            for (AnalysisStatement statement1:list1){
                object = new JSONObject();
                object.put("title",statement1.getAnalysisCode()+"<br>"+statement1.getAnalysisCell());
                object.put("colspan",statement1.getColspan());
                object.put("align","center");
                object.put("valign","middle");
                array1.add(object);
            }
        }
        array.add(array1);
        // 第二级
        List<AnalysisStatement> list2 = analysisStatementMapper.queryAnalysisName(map);
        if( list2 != null && list2.size() > 0 ){
            for (AnalysisStatement statement2:list2){
                object = new JSONObject();
                object.put("title",statement2.getAnalysisName());
                object.put("colspan",1);
                object.put("align","center");
                object.put("valign","middle");
                array2.add(object);
                object = new JSONObject();
                object.put("title",statement2.getUnit());
                object.put("colspan",1);
                object.put("align","center");
                object.put("field",statement2.getAnalysisCell()+statement2.getAnalysisName());
                object.put("valign","middle");
                map.put("analysisCell",statement2.getAnalysisCell());
                map.put("analysisName",statement2.getAnalysisName());
                data = analysisStatementMapper.queryAnalysisResult(map);
                arrayData = new JSONArray();
                if(data != null && data.size()>0){
                    for (AnalysisStatement statement3:data){
                        arrayData.add(statement3.getAnalysisResult());
                    }
                }
                object.put("data",arrayData);
                array3.add(object);
            }
        }
        array.add(array2);
        array.add(array3);
        return array;
    }

    @Override
    public JSONArray perList(String startTime, String endTime) {
        Map map = new HashMap<>();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        JSONArray array = new JSONArray();
        JSONArray array1 = new JSONArray();
        JSONArray array2 = new JSONArray();
        JSONArray array3 = new JSONArray();
        JSONArray arrayData;
        JSONObject object;
        int colspan;
        // 第一级
        List<AnalysisStatement> list1 = analysisStatementMapper.queryPerAnalysisCell(map);
        if( list1 != null && list1.size() > 0 ){
            for (AnalysisStatement statement1:list1){
                object = new JSONObject();
                object.put("title",statement1.getAnalysisCode()+"<br>"+statement1.getAnalysisCell());
                object.put("colspan",statement1.getColspan());
                object.put("align","center");
                object.put("valign","middle");
                array1.add(object);
            }
        }
        array.add(array1);
        // 第二级
        List<AnalysisStatement> list2 = analysisStatementMapper.queryPerAnalysisName(map);
        if( list2 != null && list2.size() > 0 ){
            for (AnalysisStatement statement2:list2){
                object = new JSONObject();
                object.put("title",statement2.getAnalysisName());
                object.put("colspan",1);
                object.put("align","center");
                object.put("valign","middle");
                array2.add(object);
                object = new JSONObject();
                object.put("title",statement2.getUnit());
                object.put("colspan",1);
                object.put("align","center");
                object.put("field",statement2.getAnalysisCell()+statement2.getAnalysisName());
                object.put("valign","middle");
                map.put("analysisCell",statement2.getAnalysisCell());
                map.put("analysisName",statement2.getAnalysisName());
                arrayData = new JSONArray();
                colspan = analysisStatementMapper.queryPerAnalysisCount(map).getColspan();
                if( colspan > 7 ){
                    arrayData.add(new BigDecimal(colspan/7).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue() + "/d");
                }else{
                    arrayData.add(colspan + "/w");
                }
                object.put("data",arrayData);
                array3.add(object);
            }
        }
        array.add(array2);
        array.add(array3);
        return array;
    }

    @Override
    public JSONObject monthList(String month) {
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject rowObj;
        Map map = new HashMap<>();
        map.put("day",month);
        // 第一级
        List<AnalysisStatement> cellList;
        AnalysisStatement maxAvgMin;
        List<AnalysisStatement> list = analysisStatementMapper.queryAnalysisName(map);
        if(list != null && list.size()>0){
            for(AnalysisStatement analysisStatement:list){
                rowObj = new JSONObject();
                rowObj.put("analysisCodeCell",analysisStatement.getAnalysisCodeCell());
                rowObj.put("analysisName",analysisStatement.getAnalysisName());
                rowObj.put("unit",analysisStatement.getUnit());
                rowObj.put("analysisId",analysisStatement.getAnalysisId());
                map.put("analysisCell",analysisStatement.getAnalysisCell());
                map.put("analysisName",analysisStatement.getAnalysisName());
                map.put("analysisCode",analysisStatement.getAnalysisCode());
                cellList = analysisStatementMapper.queryMonthResult(map);
                if( cellList != null && cellList.size() > 0 ){
                    for ( AnalysisStatement cell:cellList ){
                        rowObj.put(cell.getAnalysisDate(),cell.getAnalysisResult());
                    }
                }
                maxAvgMin = analysisStatementMapper.queryMonthMaxMinAvg(map);
                if( maxAvgMin != null ){
                    rowObj.put("maxValue",maxAvgMin.getMaxValue());
                    rowObj.put("minValue",maxAvgMin.getMinValue());
                    rowObj.put("avgValue",maxAvgMin.getAvgValue());
                }
                array.add(rowObj);
            }
        }
        obj.put("rows",array);
        obj.put("total",array.size());
        return obj;
    }

    @Override
    public JSONObject monthCompareList(String month) {
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject rowObj;
        Map map = new HashMap<>();
        map.put("day",month);
        // 第一级
        List<AnalysisStatement> cellList;
        List<AnalysisStatement> compareList;
        List<AnalysisStatement> list = analysisStatementMapper.queryAnalysisName(map);
        if(list != null && list.size()>0){
            for(AnalysisStatement analysisStatement:list){
                // 化验分析结果
                rowObj = new JSONObject();
                rowObj.put("analysisCodeCell",analysisStatement.getAnalysisCodeCell());
                rowObj.put("analysisName",analysisStatement.getAnalysisName());
                rowObj.put("unit",analysisStatement.getUnit());
                rowObj.put("analysisId",analysisStatement.getAnalysisId());
                rowObj.put("status",1);
                map.put("analysisCell",analysisStatement.getAnalysisCell());
                map.put("analysisName",analysisStatement.getAnalysisName());
                map.put("analysisCode",analysisStatement.getAnalysisCode());
                map.put("analysisId",analysisStatement.getAnalysisId());
                cellList = analysisStatementMapper.queryMonthResult(map);
                if( cellList != null && cellList.size() > 0 ){
                    for ( AnalysisStatement cell:cellList ){
                        rowObj.put(cell.getAnalysisDate(),cell.getAnalysisResult());
                    }
                }
                array.add(rowObj);
                // 在线分析结果
                rowObj = new JSONObject();
                rowObj.put("analysisCodeCell",analysisStatement.getAnalysisCodeCell());
                rowObj.put("analysisName",analysisStatement.getAnalysisName());
                rowObj.put("unit",analysisStatement.getUnit());
                rowObj.put("analysisId",analysisStatement.getAnalysisId());
                rowObj.put("status",2);
                compareList = analysisStatementMapper.queryCompareResult(map);
                if( compareList != null && compareList.size() > 0 ){
                    for ( AnalysisStatement compare:compareList ){
                        rowObj.put(compare.getAnalysisDate(),compare.getAnalysisResult());
                    }
                }
                array.add(rowObj);
            }
        }
        obj.put("rows",array);
        obj.put("total",array.size());
        return obj;
    }

    @Override
    public String getMonthDay(String month) {
        String result = "";
        String startDay = month+"-01";
        String endDay = DateUtil.StringToString(DateUtil.addMonth(startDay,1), DateStyle.YYYY_MM_DD);
        String nowDay = startDay;
        while (!nowDay.equals(endDay)){
            result +=nowDay+",";
            nowDay = DateUtil.addDay(nowDay,1);
        }
        result = result.substring(0,result.length()-1);
        return result;
    }

    @Override
    public int saveData(AnalysisStatement analysisStatement, String userId) {
        AnalysisStatement historyData = analysisStatementMapper.queryByAs(analysisStatement);
        Map map = new HashMap<>();
        map.put("analysisResult",analysisStatement.getAnalysisResult());
        map.put("userId",userId);
        int i;
        if(historyData == null){
            map.put("analysisId",analysisStatement.getAnalysisId());
            map.put("analysisDate",analysisStatement.getAnalysisDate());
            map.put("analysisCompareId", UniqueUtil.uuid());
            i = analysisStatementMapper.insertAs(map);
        }else{
            map.put("analysisCompareId", historyData.getAnalysisCode());
            i = analysisStatementMapper.updateAs(map);
        }
        return i;
    }

}
